﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Inbox.Forms
{
    public partial class frmCreatePlanning : Form
    {
        public enum PlanningStatus : int { Survey = 0, Maintenance = 1, Reparing = 2 }

        #region **Private**
        protected bool InEdit = false;
        private TabPage _selectedPage = null;
        private Guid selectedID = Guid.Empty;

        #endregion

        #region **Readonly**
        private static readonly string PLANNING_ID = "colID";

        #endregion

        private PlanningSVC.PlanningServiceClient svcPlanning;

        public frmCreatePlanning()
        {
            InitializeComponent();
            this.InitEnvents();
            this.gbxSaveCancle.Visible = false;
        }

        private void InitEnvents()
        {
            this.grvPlanning.SelectionChanged += new EventHandler(grvPlanning_SelectionChanged);
            this.grvPlanning.CellMouseDoubleClick += new DataGridViewCellMouseEventHandler(grvPlanning_CellMouseDoubleClick);
        }

        private void LapKeHoachKhaoSatForm_Load(object sender, EventArgs e)
        {
            this.svcPlanning = new PlanningSVC.PlanningServiceClient();
            this.InitMainGrid();
            this.InitMachinePlanGrid();
            this.BindingDataToPlanningGrid(this.svcPlanning.GetListPlanning());
        }

        private void grvPlanning_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.tabCrt.SelectedTab = this.tabDetail;
            this.selectedID = this.GetSelectedID();
            this.LoadDetail(this.selectedID);
        }

        private void grvPlanning_SelectionChanged(object sender, EventArgs e)
        {
            this.selectedID = this.GetSelectedID();
        }

        private void tabCrt_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._selectedPage = this.tabCrt.SelectedTab;

            if (!InEdit)
                this.LoadDetail(this.selectedID);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.FormToNewMode();
        }

        private void FormToNewMode()
        {
            this.InEdit = true;
            this.tabCrt.SelectedTab = this.tabDetail;

            this.txtTitle.ReadOnly = false;
            this.txtPerson.ReadOnly = false;
            this.txtDescription.ReadOnly = false;

            this.grvPlanMachine.Columns["colSelect"].ReadOnly = false;
            this.grvPlanMachine.Columns["colSelect"].Visible = true;
            this.grvPlanMachine.DataSource = this.svcPlanning.GetMachineForNew();

            this.gbxSaveCancle.Visible = true;
            this.gbxManage.Visible = false;

            this.txtDescription.Text = string.Empty;
            this.txtPerson.Text = string.Empty;
            this.txtDescription.Text = string.Empty;

        }

        private void FormToNormalMode()
        {
            this.InEdit = false;

            this.txtTitle.ReadOnly = true;
            this.txtPerson.ReadOnly = true;
            this.txtDescription.ReadOnly = true;

            this.grvPlanMachine.Columns["colSelect"].ReadOnly = true;
            this.grvPlanMachine.Columns["colSelect"].Visible = false;

            this.gbxSaveCancle.Visible = false;
            this.gbxManage.Visible = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.svcPlanning.DeletePlan(this.selectedID))
            {
                this.BindingDataToPlanningGrid(this.svcPlanning.GetListPlanning());
                this.tabCrt.SelectedTab = tabList;
                MessageBox.Show("Delete success");
            }
            else
            {
                MessageBox.Show("Delete unsuccess");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.FormToNormalMode();
            this.LoadDetail(this.selectedID);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Dictionary<Guid, string> dicMachine = new Dictionary<Guid, string>();
            foreach (DataGridViewRow row in this.grvPlanMachine.Rows)
            {
                bool selected = Convert.ToBoolean(row.Cells["colSelect"].Value);
                if (selected)
                {
                    Guid machineID = (Guid)row.Cells["colID"].Value;
                    string desp = row.Cells["colDescription"].Value == null ? string.Empty : row.Cells["colDescription"].Value.ToString();
                    dicMachine.Add(machineID, desp);
                }
            }

            bool success = this.svcPlanning.SavePlan(this.txtDescription.Text, this.txtTitle.Text,
                this.txtPerson.Text,
                dtpFrom.Value,
                dtpTo.Value,
                (int)PlanningStatus.Survey,
                0,
                dicMachine);
            if (success)
            {
                this.FormToNormalMode();
                this.grvPlanning.DataSource = this.svcPlanning.GetListPlanning();
                this.tabCrt.SelectedTab = this.tabList;
            }
            else
            {
                MessageBox.Show(this, "Không thể lưu thành công", "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region **METHODS**
        private void InitMainGrid()
        {
            DataGridViewColumn colID = new DataGridViewTextBoxColumn()
            {
                Name = "colID",
                DataPropertyName = "ID",
                Visible = false
            };

            DataGridViewColumn colPerson = new DataGridViewTextBoxColumn()
            {
                Name = "colPerson",
                HeaderText = "NGƯỜI LẬP",
                DataPropertyName = "ResponsiblePerson"
            };
            DataGridViewColumn colTitle = new DataGridViewTextBoxColumn()
            {
                Name = "colTitle",
                HeaderText = "TIÊU ĐỀ",
                DataPropertyName = "Title",
                Width = 150
            };
            DataGridViewColumn colStartDate = new DataGridViewTextBoxColumn()
            {
                Name = "colStartDate",
                HeaderText = "NGÀY BẮT ĐẦU KHẢO SÁT",
                DataPropertyName = "StartDate",
                Width = 190
            };
            DataGridViewColumn colEndDate = new DataGridViewTextBoxColumn()
            {
                Name = "colEndDate",
                HeaderText = "NGÀY DỰ KIẾN KẾT THÚC",
                DataPropertyName = "EndDate",
                Width = 167
            };

            this.grvPlanning.Columns.Add(colID);
            this.grvPlanning.Columns.Add(colPerson);
            this.grvPlanning.Columns.Add(colTitle);
            this.grvPlanning.Columns.Add(colStartDate);
            this.grvPlanning.Columns.Add(colEndDate);

            this.grvPlanning.AutoGenerateColumns = false;
            this.grvPlanning.ReadOnly = true;
        }

        private void InitMachinePlanGrid()
        {
            DataGridViewColumn colID = new DataGridViewTextBoxColumn()
            {
                Name = "colID",
                DataPropertyName = "ID",
                Visible = false
            };

            DataGridViewColumn colSelect = new DataGridViewCheckBoxColumn()
            {
                Name = "colSelect",
                Visible = false
            };

            DataGridViewColumn colName = new DataGridViewTextBoxColumn()
            {
                Name = "colName",
                HeaderText = "TÊN MÁY",
                DataPropertyName = "Name",
                ReadOnly = true
            };
            DataGridViewColumn colAmount = new DataGridViewTextBoxColumn()
            {
                Name = "colAmount",
                HeaderText = "GÍA TRỊ THỰC",
                DataPropertyName = "NetAmount",
                Width = 150,
                ReadOnly = true
            };
            DataGridViewColumn colStartDate = new DataGridViewTextBoxColumn()
            {
                Name = "colStartDate",
                HeaderText = "NGÀY BẮT ĐẦU SỬ DỤNG",
                DataPropertyName = "StartUsedDate",
                Width = 190,
                ReadOnly = true
            };
            DataGridViewColumn colDescription = new DataGridViewTextBoxColumn()
            {
                Name = "colDescription",
                HeaderText = "GHI CHÚ",
                DataPropertyName = "Description",
                Width = 167
            };

            this.grvPlanMachine.Columns.Add(colID);
            this.grvPlanMachine.Columns.Add(colSelect);
            this.grvPlanMachine.Columns.Add(colName);
            this.grvPlanMachine.Columns.Add(colAmount);
            this.grvPlanMachine.Columns.Add(colStartDate);
            this.grvPlanMachine.Columns.Add(colDescription);

            this.grvPlanMachine.AutoGenerateColumns = false;
        }

        private void BindingDataToPlanningGrid(IList data)
        {
            this.grvPlanning.DataSource = data;
        }

        private void LoadDetail(Guid PlainID)
        {
            PlanningSVC.PlanningContract planning = this.svcPlanning.GetPlanning(PlainID);

            this.txtPerson.Text = planning.ResponsiblePerson;
            this.txtTitle.Text = planning.Title;
            this.txtDescription.Text = planning.Description;

            this.grvPlanMachine.DataSource = this.svcPlanning.GetMachineLinked(PlainID);
        }

        private Guid GetSelectedID()
        {
            if (this.grvPlanning.SelectedCells == null || this.grvPlanning.SelectedCells.Count <= 0) return Guid.Empty;
            int index = this.grvPlanning.SelectedCells[0].RowIndex;

            return (Guid)this.grvPlanning.Rows[index].Cells[PLANNING_ID].Value;
        }

        #endregion

        ~frmCreatePlanning()
        {
            this.svcPlanning.Close();
        }
    }
}
